﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminRegion : System.Web.UI.Page
    {
        public Int32 regionID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RegionID"] != null)
                {
                    try
                    {
                        regionID = Int32.Parse(Request.QueryString["RegionID"].ToString());
                    }
                    catch (Exception ex)
                    {
                        regionID = -1;
                    }
                }

                if (regionID == -1)
                {
                    ViewState["action"] = HangSo.ACTION_ADD;

                }
                else
                {
                    Region theRegion = RegionProvider.GetRegionByRegionID(regionID);

                    if (theRegion == null)
                        Response.Redirect("AdminRegions.aspx");
                    else
                    {
                        this.txtDescription.Text = theRegion.Description;
                        this.txtRegionCode.Text = theRegion.RegionCode;
                        this.txtRegionName.Text = theRegion.RegionName;
                        //this.cbDeletedFlag.Checked = theRegion.DeletedFlag;
                        this.txtServiceCost.Text = theRegion.ServiceCost.ToString();
                        ViewState["ID"] = theRegion.RegionID;
                        ViewState["action"] = HangSo.ACTION_UPDATE;
                    }
                }
            }
        }

        private Boolean ValidateData()
        {
            String message = "";
            Boolean isSuccess = true;

            try
            {
                float value = float.Parse(this.txtServiceCost.Text.Trim());
            }
            catch (Exception ex)
            {
                isSuccess = false;
                message += "Tjenesten Cost må er nummer";
            }

            if (!isSuccess)
                ltrMessage.Text = message;

            return isSuccess;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            String action = ViewState["action"].ToString();

            if (!ValidateData())
                return;

            Region theRegion = new Region();
           // theRegion.DeletedFlag = this.cbDeletedFlag.Checked;
            theRegion.Description = this.txtDescription.Text.Trim();
            theRegion.RegionCode = this.txtRegionCode.Text;
            theRegion.RegionName = this.txtRegionName.Text;

            try
            {
                theRegion.ServiceCost = float.Parse(this.txtServiceCost.Text.Trim());
            }
            catch (Exception ex)
            {
                theRegion.ServiceCost = 0;
            }

            if (action == HangSo.ACTION_ADD)
            {
                theRegion.RegionID = -1;
                RegionProvider.UIRegion(theRegion, true);
            }
            else
            {
                theRegion.RegionID = Int32.Parse(ViewState["ID"].ToString());
                RegionProvider.UIRegion(theRegion, false);
            }

            Response.Redirect("AdminRegions.aspx");
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminRegions.aspx");
        }
    }

}